//
//  SC_BarChartView.m
//  SCCharts
//
//  Created by Yang on 9/9/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SC_BarChartView.h"
#import "SC_BarChartData.h"

#pragma mark static parameters

static float ScrollViewTopMargin = 50.0f;
static float ScrollViewBottomMargin = 30.0f;
static float ScrollViewLeftMargin = 0.0f;
static float ScrollViewRightMargin = 0.0f;

static int HighestValue = 0;
static float YAxisLineLength = 8.0f;

@implementation SC_BarChartView


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
		self.backgroundColor = [UIColor whiteColor];
		dataArray = [[NSMutableArray alloc] init];
		_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(ScrollViewLeftMargin, ScrollViewTopMargin,
																	 frame.size.width - ScrollViewLeftMargin - ScrollViewRightMargin,
																	 frame.size.height - ScrollViewTopMargin)];
		_scrollView.contentSize = _scrollView.frame.size;
		_scrollView.scrollEnabled = YES;
		_scrollView.userInteractionEnabled = YES;
		_scrollView.showsHorizontalScrollIndicator = NO;
		_scrollView.showsVerticalScrollIndicator = NO;
		[self addSubview:_scrollView];
		
		CGRect rect = _scrollView.bounds;
		
		plotView = [[SC_BarChartPlotView alloc] initWithFrame:rect];
//		CGRect rect = _scrollView.bounds;
//		rect.size.height += [plotView marginHeightWithScrollView];
//		plotView.frame = rect;
		plotView.delegate = self;
		[_scrollView addSubview:plotView];
    }
    return self;
}

- (void)setData:(NSArray *)array {
	[dataArray setArray:array];
	[self calculateHighestValue];
	[self setNeedsDisplay];
	[plotView resetIndicator];
	[plotView setData:array];
	[plotView setNeedsDisplay];
}
- (void)calculateHighestValue {
	for (SC_BarChartData *data in dataArray) {
		for (NSString *string in data.data) {
			if (HighestValue < [string intValue]) {
				HighestValue = [string intValue];
			}
		}
	}
	int temp = HighestValue;
	if (temp%3 != 0) {
		int x = HighestValue / 3;
		HighestValue = (x + 1) * 3;
	}
}

#pragma mark Drawing Methods
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
	if ([dataArray count] < 1) {
		return;
	}
	CGContextRef context = UIGraphicsGetCurrentContext();
	[self drawYAxisLabels:context];
	[self drawXAxisLabels:context];
}

- (void)drawYAxisLabels:(CGContextRef)context {
	
	float margin = (self.frame.size.height - ScrollViewBottomMargin - ScrollViewTopMargin) / 3.0f;
	
	CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
	CGContextSetLineWidth(context, 1.0);
	CGPoint strokeSegments[] =
	{
		CGPointMake(ScrollViewLeftMargin - 1.0f,ScrollViewTopMargin),
		CGPointMake(ScrollViewLeftMargin - 1.0f, self.frame.size.height - ScrollViewBottomMargin),
		CGPointMake(ScrollViewLeftMargin - 1.0f - YAxisLineLength, ScrollViewTopMargin),
		CGPointMake(ScrollViewLeftMargin - 1.0f, ScrollViewTopMargin),
		CGPointMake(ScrollViewLeftMargin - 1.0f - YAxisLineLength, ScrollViewTopMargin + margin * 1),
		CGPointMake(ScrollViewLeftMargin - 1.0f, ScrollViewTopMargin + margin * 1),
		CGPointMake(ScrollViewLeftMargin - 1.0f - YAxisLineLength, ScrollViewTopMargin + margin * 2),
		CGPointMake(ScrollViewLeftMargin - 1.0f, ScrollViewTopMargin + margin * 2),
		CGPointMake(ScrollViewLeftMargin - 1.0f - YAxisLineLength, ScrollViewTopMargin + margin * 3),
		CGPointMake(ScrollViewLeftMargin - 1.0f, ScrollViewTopMargin + margin * 3),
	};
	CGContextStrokeLineSegments(context, strokeSegments, sizeof(strokeSegments)/sizeof(strokeSegments[0]));
	CGContextStrokePath(context);
	
	NSInteger step = HighestValue / 3;
	for (int i = 0; i < 4; i ++) {
		NSString *string = [NSString stringWithFormat:@"%d",step * i];
		CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:14.0f]];
		[string drawAtPoint:CGPointMake(ScrollViewLeftMargin - 1.0f - size.width - YAxisLineLength, self.frame.size.height - ScrollViewBottomMargin - i * margin - size.height / 2)
				   withFont:[UIFont systemFontOfSize:14.0f]];
	}
	
}
- (void)drawXAxisLabels:(CGContextRef)context {
}

- (void)dealloc {
	[_scrollView release];
	[dataArray release];
	
	[plotView release];
    [super dealloc];
}

#pragma mark SC_BarChartPlotViewDelegate
- (void)plotViewSizeChangedTo:(CGSize)size {
	//NSLog(@"size %.0f, %.0f",size.width,size.height);
	_scrollView.contentSize = size;
}

@end
